package integral

import (
	"context"
	"gitee.com/enjoy_5/integral-rpc/internal/server"
	"gitee.com/enjoy_5/integral-rpc/proto/integral"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type IntegralServer struct {
	integral.UnimplementedIntegralServer
}

func (i *IntegralServer) GetIntegralInfo(ctx context.Context, in *integral.GetIntegralInfoRequest) (*integral.GetIntegralInfoResponse, error) {
	if in.UserId <= 0 {
		return nil, status.Error(codes.FailedPrecondition, "UserId can not be 0")
	}

	id, err := server.GetIntegralInfoByUserId(in.UserId)
	if err != nil {
		return nil, err
	}

	return &integral.GetIntegralInfoResponse{Info: id}, nil
}
func (i *IntegralServer) CreateIntegralRecordInfo(ctx context.Context, in *integral.CreateIntegralRecordInfoRequest) (*integral.CreateIntegralRecordInfoResponse, error) {
	if in.GetIn().Describe == "" {
		return nil, status.Error(codes.FailedPrecondition, "Describe can not be 0")
	}
	if in.GetIn().UserId <= 0 {
		return nil, status.Error(codes.FailedPrecondition, "UserId can not be 0")
	}

	record, err := server.CreateIntegralRecord(in.GetIn())
	if err != nil {
		return nil, err
	}

	return &integral.CreateIntegralRecordInfoResponse{Info: record}, nil
}
func (i *IntegralServer) GetIntegralRecordInfoList(ctx context.Context, in *integral.GetIntegralRecordInfoListRequest) (*integral.GetIntegralRecordInfoListResponse, error) {
	if in.UserId <= 0 {
		return nil, status.Error(codes.FailedPrecondition, "UserId can not be 0")
	}

	list, err := server.GetIntegralRecordInfoByUserId(in.UserId)
	if err != nil {
		return nil, err
	}

	return &integral.GetIntegralRecordInfoListResponse{Info: list}, nil
}
